<?php


namespace app\common\model;


use think\Db;
use think\Model;

class RechargeBill extends Model
{
    // 表名
    protected $name = 'recharge_bill';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];


    //批量检测分账结果
    public static function checkFen()
    {
        $map = [
            'is_send' => 1,
            'is_check'=>0,
            'is_success' => 0,
        ];
        $end = time()-300;
        $list = self::where($map)
            ->limit(100)
            ->where('createtime' , '<' ,$end)
            ->select();
        foreach ($list as $v){
            $row = AgentShare::checkFen($v,1);
        }
    }

    //执行分账
    public static function sendFen()
    {
        $map = [
            "is_send" => 0,
        ];
        $list = self::where($map)
            ->limit(100)
            ->select();
        foreach($list as $v){
            if($v['mchId'] && $v['fz_number']){
                //建立账号绑定关系
                //$res = AgentShare::bindAccount($v['mchId'],$v['fz_number']);
                //if($res){
                    //发放分账
                    AgentShare::sendFen($v,1);
                    Db::table('recharge_bill')->where('id',$v['id'])->update(['is_send' => 1]);
                //}
            }
        }
    }

    // 执行充值未购物 的充值记录分账
    public static function findNoOrderUser()
    {
        $map = [
            'status' => 1,
            'pay_type'=>'wallet'
        ];
        $endtime = strtotime(date('Y-m-d'));
        $start = $endtime - 24*3600;
        $list = Db::table('shopro_order')->where($map)
            ->where('createtime','>',$start)
            ->where('createtime','<',$endtime)
            ->field('id,user_id')
            ->select();
        $userids_array = array_column($list,'user_id');
     /*   print_r($userids_array);*/
        $map = [
            'isNeedShare' => 1,
            'is_fen' => 0,
        ];
        $list = Db::table('recharge')->where($map)
            ->whereNotIn('user_id',$userids_array)
            //->where('createtime','>',$start)
            //->where('createtime','<',$endtime)
            ->select();
        foreach ($list as $v){
            AgentShare::findFenMoney($v['mchId'],$v['chnltrxid']);
        }
        /*echo Db::table('recharge')->getLastSql();
        print_r($list);die();*/
        //echo count($list);
        //foreach($list as $v){}
        self::createFenLog($list);
        //计算误差
        foreach ($list as $v){
            $map = [
                'recharge_id' => $v['id'],
            ];
            $row = self::where($map)->order('fen_fee desc')->find();
            if($row){
               // if($v['']){}
            }
        }

    }

    public static function createFenLog($list)
    {
        $userlist = Reword::rechargeFenConfig();
        foreach($list as $v){
            //AgentShare::findFenMoney($v['mchId'],$v['chnltrxid']);
            $row = Db::table('recharge')->where('id',$v['id'])->find();
            if($row){
                if($row['sharableAmount'] >0){
                    foreach ($userlist as $fen)
                    self::log($v,$row['sharableAmount'],$fen);
                }
                $up = [
                    'is_fen' => 2,
                    'no_buy' => 1,
                ];
                Db::table('recharge')->where('id',$v['id'])->update($up);
            }
        }
    }

    public static function log($v,$amount,$user){
        $fen_fee = round(($amount*($user['percent']/100)),2);
        $log = [
            'user_id' => $v['user_id'],
            'recharge_id' => $v['id'],
            'fen_sn' => create_sn('F'),
            'total_fee' => $amount,
            'fz_title' => $user['fz_title'].$user['percent'],
            'percent' => $user['percent'],
            'fz_number' => $user['fz_number'],
            'fen_fee' => $fen_fee,
            'mchId' => $v['mchId'],
            'chnltrxid' => $v['chnltrxid'],
        ];
        self::create($log);
    }

    public static function createFenLog2($list)
    {
        $userlist = Reword::rechargeFenConfig();
        foreach($list as $v){
            if($v['is_fen'] == 1 && $v['have_fee'] > 0){
                foreach ($userlist as $fen)
                    self::log($v,$v['have_fee'],$fen);
            }
            if($v['is_fen'] == 0 && $v['sharableAmount'] >0){
                foreach ($userlist as $fen)
                    self::log($v,$v['sharableAmount'],$fen);
            }
            $up = [
                'is_fen' => 2,
                'no_buy' => 1,
            ];
            Db::table('recharge')->where('id',$v['id'])->update($up);
        }

    }
}
